﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TEKVR02
    { 
        public TEKVR02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVRKOPF();
            DataContext = MV;
            InitializeComponent();
            AUFTYP = "B";
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
            AUFTYP = "B";
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KUNDNR = "";
                VVKP = "";
                KEY = "";
                ZAHLBED = "";

            }
            else
            {
                KEY = dwMaster["VRKRENR"].ToString();
                KUNDNR = dwMaster["VRKKUNDNR"].ToString();
                VVKP = dwMaster["VRPAUFKRS"].ToString() + dwMaster["VRPAUFLFD"].ToString();
                ZAHLBED = dwMaster["VRKZAHLBED"].ToString();
            }
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                return;
            }
            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            LFD = VSDDAT.SD22.GETNEXT("0022", "MA", "VRRECHNR", 2);
            dwData["VRKRENR"] = AUFART + Helpers.Helper.Right("00000000" + LFD, 18);
            dwData["VRKVB"] = KRS.PadRight(3);
            dwData["VRKREART"] = AUFART.PadRight(2);  
            TMV.Insert(dwData);
            KEY = "";
            KEY = dwData["VRKRENR"].ToString();
            IsNew = false;
        }

        public void VRNewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                return;
            }

            IsNew = false;
        }

        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            switch (app.company)
            {
                case "TST":
                    KRS = MV.year.Substring(2, 2);
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            //KRS = "S" + MV.year.Substring(3, 1);
                            break;
                        case "TMODE":
                            //KRS = "M" + MV.year.Substring(3, 1);
                            break;

                    }
                    break;
            }
        }

        private void AUF_Enter(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 9)
                return;
            AUFTYP = ORDERNUM.Substring(0, 1);
            KRS = ORDERNUM.Substring(1, 2);
            LFD = ORDERNUM.Substring(3, 6);
            if (!IsLoaded)
                return;

            dwData = TBMain.NewRow();
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKVERBNR"] = ORDERNUM.Substring(1, 8);
            dwData["VRKZESSION"] = AUFTYP;
            SResult = TMV.QVRK06(dwData);
            MergeData(SResult, true, true);
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            if (LFD == "000000") LFD = "";
            dwData["VRKMANDANT"] = app.Mandant;
            dwData["VRKVERBNR"] = " AND VRKVERBNR LIKE'" + KRS + LFD + "%'"; ;
            dwData["VRKZESSION"] = " AND VRKZESSION='" + AUFTYP + "'";
            dwData["VRKZAHLBED"] = " AND VRKZAHLBED LIKE '" + ZAHLBED + "%'";
            SResult = TMV.QVRK04(dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                TMV.ZHLBEDCHGEK(row);
            }
            MergeData(SResult, true, true);
            base.QueryCommand(sender, e);
        }
    

        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            vwMaster.View.MoveCurrentTo(-1);
        }

        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            base.bgWorker_DoWork(sender, e);
            EventDispatcher.Instance.RaiseStatus("开始留位", StatusEventType.StopProgress);
            if (IsEK)
            {
                TMV.EKKPT(PM);
            }
            if (IsVV)
            {
                TMV.VVEKVR(PM);
            }
            if (IsRest)
            {
                TMV.VV2EKREST(PM);
            }
            if (IsVR)
            {
                TMV.EKKPT(PM);
                TMV.REFRESHVR(PM);
            }
        }

        public void EKKStartPaymentCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 1;
            PM["AUFTYP"] = "B";
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ZAHLBED"] = ZAHLBED;
            PM["AUFTYP"] = "B";
            IsVR = false;
            IsVV = false;
            IsEK = true;
            IsRest = false;
            bgWorker.RunWorkerAsync();
        }

        public void VVStartPaymentCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 2;
            PM["AUFTYP"] = "B";
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ZAHLBED"] = ZAHLBED;
            IsVR = false;
            IsVV = true;
            IsEK = false;
            IsRest = false;
            bgWorker.RunWorkerAsync();
        }

        public void RESTPaymentCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一个发票行");
                return;
            }
            PAYPAL = 3;
            PM["AUFTYP"] = "B";
            PM["RENR"] = KEY;
            PM["PAYPAL"] = PAYPAL;
            PM["ORDERNUM"] = VVKP;
            PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
            PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
            PM["ZAHLBED"] = ZAHLBED;
            IsVR = false;
            IsVV = false;
            IsEK = false;
            IsRest = true;
            bgWorker.RunWorkerAsync();
        }

        bool IsVR = false;
        bool IsVV = false;
        bool IsEK = false;
        bool IsRest = false;

        public void RefreshVRCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                {
                    MessageBox.Show("请选择一个发票行");
                    return;
                }
                PM["AUFTYP"] = "B";
                PM["RENR"] = KEY;
                PM["PAYPAL"] = PAYPAL;
                PM["ORDERNUM"] = VVKP;
                PM["KRS"] = dwMaster["VRPAUFKRS"].ToString();
                PM["LFD"] = dwMaster["VRPAUFLFD"].ToString();
                PM["ZAHLBED"] = ZAHLBED;
                IsVR = true;
                IsVV = false;
                IsEK = false;
                IsRest = false;
                bgWorker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            //增加附加费和尾款的费用管理
        } 
      
    }
}
